package com.hdax.dm.order.controller;

import com.hdax.dm.common.exception.DmException;
import com.hdax.dm.order.service.OrderService;
import com.hdax.dm.order.vo.SubmitOrderVo;
import com.hdax.dm.token.TokenUtil;
import com.hdax.dm.utils.CommonResponse;
import com.hdax.dm.utils.ResponseUtil;
import com.hdax.dm.utils.Token;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping(path = "/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    //公钥
    @Value("${jwt.rsa_public_key}")
    private String RSA_PUBLIC_KEY;
    @Autowired
    private TokenUtil tokens;

    @PostMapping("/p/submitorder")
    public CommonResponse submitorder(@RequestBody SubmitOrderVo vo, HttpServletRequest request) throws DmException {
        // 获取请求消息中的所有头字段
        String  token  = request.getHeader("token");
        //解密
//        Claims claims = Token.token(token,RSA_PUBLIC_KEY);
//        Long userId = Long.valueOf(claims.getId());
        //解密
        Long userId = Long.parseLong(tokens.token(token));
        if(userId==0L) throw new DmException("1010","登录失效，清重新登录");
        return orderService.submitOrder(userId,vo);
    }
    /**
     * 取消订单
     * @param params
     * @return
     */
    @PostMapping("/p/cancelOrder")
    public CommonResponse cancelOrder(@RequestBody Map<String,String> params){
        return orderService.cancelOrder(params.get("orderNo"));
    }
}
